package com.ithe.shopweb.controller;

import com.ithe.shopweb.common.Constants;
import com.ithe.shopweb.common.Result;
import com.ithe.shopweb.pojo.Home;
import com.ithe.shopweb.pojo.resp.HomeResp;
import com.ithe.shopweb.serve.HomeServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 日期： 2024/5/25 0:43
 */

@RestController
@RequestMapping("/home")
public class HomeController implements Constants {

    @Autowired
    private HomeServe homeServe;

    @PostMapping("/offerQuery")
    public Result offerQuery(@RequestBody Home home) {
        String offerLocal = home.getOfferLocal();
        String offerStyle = home.getOfferStyle();
        Double minPrice = home.getMinPrice();
        Double maxPrice = home.getMaxPrice();
        String offerDegree = home.getOfferDegree();
        String offerDiscount  = home.getOfferDiscount();
        Integer pageNum = home.getPageNum();
        Integer pageSize = home.getPageSize();

        List<HomeResp> offerResp = homeServe.offerQuery(offerLocal, offerStyle, minPrice, maxPrice, offerDegree, offerDiscount, (pageNum-1)*pageSize, pageSize);
        Integer totalNum = homeServe.offerAll().size();

        Map<String, Object> offerList = new HashMap<>();
        offerList.put("total", totalNum);
        offerList.put("data", offerResp);

        if(offerList.get("data") == null)
            return Result.error(CODE_400, "暂无任何商品");
        else
            return Result.success(offerList);
    }

    @PostMapping("/offerSearch")
    public Result offerSearch(@RequestBody Home home) {
        String str = home.getSearchStr();
        Integer pageNum = home.getPageNum();
        Integer pageSize = home.getPageSize();

        List<HomeResp> offerResp = homeServe.offerSearch(str, (pageNum-1)*pageSize, pageSize);
        Integer totalNum = offerResp.size();

        Map<String, Object> offerList = new HashMap<>();
        offerList.put("total", totalNum);
        offerList.put("data", offerResp);

        if(offerList.get("data") == null)
            return Result.error(CODE_400, "查无此类商品");
        else
            return Result.success(offerList);
    }

    @PostMapping("/cartAdd")
    public Result cartAdd(@RequestBody Home home) {
        String offerID =    home.getOfferID();
        String offerImage = home.getOfferImage();
        String offerName =  home.getOfferName();
        String offerStyle = home.getOfferStyle();
        Double offerPrice = home.getOfferPrice();
        String offerLocal = home.getOfferLocal();
        String offerDiscount = home.getOfferDiscount();
        String offerDegree =home.getOfferDegree();
        Double finalPrice = home.getFinalPrice();
        String offerUser =  home.getOfferUser();
        String cartUser =   home.getCartUser();
        Integer number = homeServe.cartAdd(offerID, offerImage, offerName, offerStyle, offerPrice, offerLocal, offerDiscount, offerDegree, finalPrice, offerUser, cartUser);
        if(number == 1) {
            homeServe.offerDelete(offerID);
            return Result.success();
        } else {
            return Result.error(CODE_500, "服务器异常");
        }

    }
}
